package com.ny.zmb.openapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ny.zmb.openapi.entity.PmUserToken;
import com.ny.zmb.openapi.mapper.PmUserTokenMapper;
import com.ny.zmb.openapi.service.IPmUserTokenService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 会员Token表 服务实现类
 * </p>
 *
 * @author xie
 * @since 2024-10-25
 */
@Service
public class PmUserTokenServiceImpl extends ServiceImpl<PmUserTokenMapper, PmUserToken> implements IPmUserTokenService {


    /**
     * 根据用户token 获取用户信息
     * @param token
     * @return
     */
    @Override
    public PmUserToken getUserDetailByToken(String token){
        if (StringUtils.isBlank(token)){
            throw new RuntimeException("登录信息已失效");
        }
        LambdaQueryWrapper<PmUserToken> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PmUserToken::getToken,token);
        wrapper.orderByDesc(PmUserToken::getExpiretime);
        List<PmUserToken> pmUserTokens = this.baseMapper.selectList(wrapper);
        if (null != pmUserTokens && pmUserTokens.size() > 0){
            return pmUserTokens.get(0);
        }
        throw new RuntimeException("登录信息已失效");
    }

}
